package com.hz.service.impl;

import com.hz.mapper.UserMapper;
import com.hz.pojo.User;
import com.hz.service.UserService;
import com.hz.utils.Md5Util;
import com.hz.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByName(String username) {
        return userMapper.selectByName(username);
    }

    @Override
    public void add(String username, String password) {
        // 先对密码进行加密
        String md5String = Md5Util.getMD5String(password);

        // 保存用户
        userMapper.insert(username,md5String);

    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    public int updateAvatarUrl(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        return userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public int updatePwd(String username, String new_pwd) {
        String md5String = Md5Util.getMD5String(new_pwd);
        return userMapper.updatePwd(username,md5String);
    }
}
